/*
 *  draw.cpp
 *  SketchMenu
 *
 *  Created by George on 4/15/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */
#include "SketchMenu.h"
#include <math.h>

using namespace std;

SketchMenu::SketchMenu(){
  last_event.type = GLUT_UP;
  last_event.tilt.x = .5;
  last_event.tilt.y = .5;
}

void SketchMenu::draw(void){
  float flatness = 2.0f;
  float hand = 1.0; //RIGHT;
  float length,theta;
  if(last_event.tilt.x < 0) hand = -1.0;
  point n = loc_to_screen(last_event.loc);
  glColor3f(1.0f,0.0f,0.0f);
  /*glBegin(GL_LINES);
  {
    glVertex3f(n.x,n.y,0.0);
    glVertex3f(n.x+(last_event.tilt.x),
                n.y+(last_event.tilt.y),0.0);
  }
  glEnd();*/
  
  float dx = last_event.tilt.x*flatness;
  float dy = last_event.tilt.y*flatness;
  theta = atan(dy/dx)+M_PI;
  
  if(hand<0){
    theta += M_PI;
  }
  float r = sqrt(dx*dx+dy*dy);
  length = r *flatness/4* M_PI/4;
  for(int i=0; i<4; i++){
    DrawMenu(NULL, i, n.x+dx,n.y+dy,r,theta,length);
  }
  
  printf("\ttheta:%f  length:%f\n",theta-length/2,length);
  printf("\tn:%f,%f\n",n.x+dx,n.y+dy);
}



void SketchMenu::handle_sketch_event(sketch_event & s){
  //if(s.type == GLUT_DOWN ){
    last_event = s;
  //}
  printf("loc- [%f,%f] [%f,%f]\n",last_event.loc.x,last_event.loc.y,last_event.tilt.x,last_event.tilt.y);
}

void SketchMenu::set_size(int width, int height){
  this->width = width;
  this->height= height;
}

point SketchMenu::loc_to_screen(point & loc){
  point n;
  n.x = 2.0f*((loc.x/(float)(this->width))-0.5f);
  n.y = 2.0f*((loc.y/(float)(this->height))-0.5f);
  
  return n;
}

void DrawMenu(Menu * menu, int ring, float x, float y, float start_r,float theta, float length){
  //float r1;
  //float r2;
  x = x + .2 * ring * cos(theta);
  y = y + .2 * ring * sin(theta);
  //r1 = start_r + ring * 0.2;
  //r2 = r1+0.22;
  
  double arcThickness = 0.2f;
  
  
  
  setColor(ring,1);
  DrawArcPolygon(x,y, -0.25*ring-0.1, start_r,start_r+arcThickness-.06,theta-length/2,length,12);
  
  //DrawArcPolygon(x,y, -0.25*ring, r1,r2,theta-length/2,length,12);
  int sections = 4;
  int selected = ring;
  
  double arcLength = (length)/sections;
  double spacer = arcLength*0.1;
  double lastTheta = theta-length/2;
  for(int i=0; i<4; i++){
    if(selected == i){
      setColor(ring+1,1);
      DrawArcPolygon(x,y, -0.25*ring, start_r+.03,start_r+arcThickness,lastTheta+spacer ,arcLength-2*spacer,6);
    }
    else{
      glColor4f(8.0f,8.0f,8.0f,1.0f);
      DrawArcPolygon(x,y, -0.25*ring, start_r+.03,start_r+arcThickness-.02,lastTheta+spacer ,arcLength-2*spacer,6);
    }
    if(selected == i){
      glColor4f(8.0f,8.0f,8.0f,1.0f);
      DrawArcPolygon(x,y, -0.25*ring, start_r+0.05,start_r+arcThickness-.04,lastTheta+2*spacer ,arcLength-4*spacer,6);
    }
    lastTheta = lastTheta + arcLength;
  }
}

void setColor(int id, float intensity){
  switch(id % 4){
    case 0:
      glColor3f(1.0f,0.2f,0.2f);
      break;
    case 1:
      glColor3f(0.2f,0.2f,1.0f);
      break;
    case 2:
      glColor3f(0.2f,1.0f,0.2f);
      break;
    case 3:
      glColor3f(1.0f,0.5f,0.0f);
      break;
  }
}

void DrawArc(float cx, float cy, float r, float start_angle, float arc_angle, int num_segments) 
{ 
	float theta = arc_angle / float(num_segments - 1);//theta is now calculated from the arc angle instead, the - 1 bit comes from the fact that the arc is open
	float tangetial_factor = tanf(theta);
	float radial_factor = cosf(theta);

	
	float x = r * cosf(start_angle);//we now start at the start angle
	float y = r * sinf(start_angle); 
    
	glBegin(GL_LINE);//since the arc is not a closed curve, this is a strip now
	for(int ii = 0; ii < num_segments; ii++)
	{ 
		glVertex2f(x + cx, y + cy);

		float tx = -y; 
		float ty = x; 

		x += tx * tangetial_factor; 
		y += ty * tangetial_factor; 

		x *= radial_factor; 
		y *= radial_factor; 
	} 
	glEnd(); 
}

void DrawArcPolygon(float cx, float cy, float z, float r1,float r2, float start_angle, float arc_angle, int num_segments){
  
  float theta = arc_angle / float(num_segments - 1);//theta is now calculated from the arc angle instead, the - 1 bit comes from the fact that the arc is open

	//float tangetial_factor = tanf(theta);

	//float radial_factor = cosf(theta);
  
  float temp;
  temp = r2;
  if(r2 > r1){
    r2 = r1;
    r1 = temp;
  }
	
	float x = r1 * cosf(start_angle);//we now start at the start angle
	float y = r1 * sinf(start_angle); 
    
	glBegin(GL_TRIANGLE_STRIP);//since the arc is not a closed curve, this is a strip now
	for(int ii = 0; ii < num_segments; ii++)
	{ 
    x = r1*cos(start_angle+theta*(ii));
    y = r1*sin(start_angle+theta*(ii));
		glVertex2f(x + cx, y + cy);
    
    x = r2*cos(start_angle+theta*(ii));
    y = r2*sin(start_angle+theta*(ii));
		glVertex3f(x + cx, y + cy, z);

	} 
   glEnd();
}
